package com.maxjay.component.metadata.support;

import com.maxjay.component.metadata.model.Entity;
import com.maxjay.component.metadata.model.EntityField;
import com.maxjay.component.metadata.model.Record;

/**
 * 实体记录处理器
 * 
 * @author zhaixm
 *
 */
public class EntityRecordProcessor {

	/**
	 * 将数据库记录转换为可显示的记录
	 * 
	 * @param entity
	 * @param storeRecord
	 * @return
	 */
	public static Record convertDisplayRecord(Entity entity, Record storeRecord) {
		Record displayRecord = new Record();
		for(String fieldName : storeRecord.fieldSet()) {
			EntityField entityField =  entity.getEntityFieldByName(fieldName);
			displayRecord.setValue(fieldName, entityField.convertDisplayValue(storeRecord));
		}
		return displayRecord;
	}

	/**
	 * 将页面或其它类型数据转换为数据库对应类型的数据
	 * 
	 * @param entity
	 * @param displayRecord
	 * @return
	 */
	public static Record convertStoreRecord(Entity entity, Record displayRecord) {
		Record storeRecord = new Record();
		for(String fieldName : displayRecord.fieldSet()) {
			EntityField entityField =  entity.getEntityFieldByName(fieldName);
			storeRecord.setValue(fieldName, entityField.convertStoreValue(storeRecord));
		}
		return storeRecord;
	}
	
}
